
//
//  JokeCollectionViewCell.m
//  text
//
//  Created by 杜昕宇 on 15/11/11.
//  Copyright © 2015年 peter. All rights reserved.
//

#import "JokeCollectionViewCell.h"
#import "MacroDefinition.h"
#import "CollectionJoke.h"
#import "CoreData.h"
@interface JokeCollectionViewCell()

@property (nonatomic, retain) CoreData *coreDataManager; // 数据库管理

@property (nonatomic, retain) UILabel *timeLabel; // 时间

@property (nonatomic, retain) UILabel *detailLabel; // 详情

@property (nonatomic, retain) UIButton *collectionImage; // 收藏

@property (nonatomic, retain) UIButton *shareImage; // 分享

@property (nonatomic, retain) UIView *lineView; // 一根线

@property (nonatomic, copy) shareBlock myBlock;
@end

@implementation JokeCollectionViewCell

- (void)dealloc
{
    Block_release(_myBlock);
    [_coreDataManager release];
    [_lineView release];
    [_myJoke release];
    [_timeLabel release];
    [_detailLabel release];
    [_collectionImage release];
    [_shareImage release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.coreDataManager = [CoreData shareManagerWithFile:@"joke"];
//        创建视图
        [self creatViews];
    }
    return self;
}

- (void)setMyJoke:(LastJoking *)myJoke{
    if (_myJoke != myJoke) {
        [_myJoke release];
        _myJoke = [myJoke retain];
        self.timeLabel.text = myJoke.created_at;
        self.detailLabel.text = myJoke.text;
    }
    NSArray *arr = [self returnArray];
    if (arr.count == 0) {
        [self.collectionImage setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
    } else {
        [self.collectionImage setImage:[UIImage imageNamed:@"liked"] forState:UIControlStateNormal];
    }
}

- (void)setSomeJoke:(SomeJoke *)someJoke{
    if (_someJoke != someJoke) {
        [_someJoke release];
        _someJoke = [someJoke retain];
    }

    NSArray *arr = [self returnArray];
    if (arr.count == 0) {
        [self.collectionImage setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
    } else {
        [self.collectionImage setImage:[UIImage imageNamed:@"liked"] forState:UIControlStateNormal];
    }

    self.timeLabel.text = someJoke.created_at;
    self.detailLabel.text = someJoke.text;
}

- (void)creatViews{
    

/************************************  创建 time  ****************************/
    self.timeLabel = [[UILabel alloc] init];
//    _timeLabel.backgroundColor = [UIColor cyanColor];
    _timeLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_timeLabel];
    [_timeLabel release];
/************************************ 创建一根线 ****************************/
    self.lineView = [[UIView alloc] init];
    _lineView.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_lineView];
    [_lineView release];

/************************************  创建 detail  ****************************/
    self.detailLabel = [[UILabel alloc] init];
//    _detailLabel.backgroundColor = [UIColor cyanColor];
    _detailLabel.numberOfLines = 0;
    [self.contentView addSubview:_detailLabel];
    [_detailLabel release];
/************************************  创建 收藏  ****************************/
    self.collectionImage = [[UIButton alloc] init];
//    _collectionImage.backgroundColor = [UIColor cyanColor];
    [_collectionImage addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.collectionImage setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
    [self.contentView addSubview:_collectionImage];
    [_collectionImage release];
/************************************  创建 分享  ****************************/
    self.shareImage = [[UIButton alloc] init];
//    _shareImage.backgroundColor = [UIColor cyanColor];
    [_shareImage addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.shareImage setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [self.contentView addSubview:_shareImage];
    [_shareImage release];
}

- (NSArray *)returnArray{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"CollectionJoke" inManagedObjectContext:_coreDataManager.managedObjectContext];
    [request setEntity:entity];
    NSString *str;
    if (_myJoke) {
        str = _myJoke.text;
    } else if (_someJoke){
        str = _someJoke.text;
    }
    request.predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"text = '%@'", str]];
    NSArray *arr = [_coreDataManager.managedObjectContext executeFetchRequest:request error:nil];
    return arr;
}

//    收藏
- (void)buttonAction:(UIButton *)button{
    NSArray *arr = [self returnArray];

    if (arr.count == 0) {
        CollectionJoke *joke = [NSEntityDescription insertNewObjectForEntityForName:@"CollectionJoke" inManagedObjectContext:_coreDataManager.managedObjectContext];
        if (_myJoke) {
            joke.text = _myJoke.text;
            joke.created_at = _myJoke.created_at;
            joke.zan = [NSNumber numberWithFloat:[joke.zan floatValue]];
        } else if (_someJoke) {
            joke.text = _someJoke.text;
            joke.created_at = _someJoke.created_at;
            joke.zan = [NSNumber numberWithFloat:[_someJoke.zan floatValue]];
        }

        [_coreDataManager saveContext];
        [button setImage:[UIImage imageNamed:@"liked"] forState:UIControlStateNormal];
        NSLog(@"收藏了");
    } else {

        for (CollectionJoke *jok in arr) {
            [[CoreData shareManagerWithFile:@"joke"].managedObjectContext deleteObject:jok];
            [_coreDataManager saveContext];
        }
        [button setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
        NSLog(@"取消收藏");
    }
}

//    分享
- (void)shareAction:(UIButton *)button{
    self.myBlock();
}
- (void)shareButtonClicked:(shareBlock)block{
    self.myBlock = block;
}
+ (CGFloat)heightOfText:(NSString *)text{

//    屏幕宽-30 (20 是 item 距离屏幕距离, 10 是 label 离左边的距离)
    CGSize size = CGSizeMake(SCREEN_WIDTH - 30, MAXFLOAT);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    CGRect label = [text boundingRectWithSize: size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return label.size.height;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat vWidth = SCREEN_WIDTH - 20;
    CGFloat vHeight = SCREEN_HEIGHT / 20;
    CGFloat imageHeight = SCREEN_HEIGHT / 30;
    self.timeLabel.frame = CGRectMake(10 , 0, vWidth - 10, vHeight);
    self.lineView.frame = CGRectMake(0, _timeLabel.Height + _timeLabel.Y , vWidth, 1);
#warning 自适应高度
    if (_myJoke) {
        self.detailLabel.frame = CGRectMake(_timeLabel.X, _timeLabel.Height + _timeLabel.Y + 5, vWidth - 10, [[self class] heightOfText:_myJoke.text]);
    } else {
        self.detailLabel.frame = CGRectMake(_timeLabel.X, _timeLabel.Height + _timeLabel.Y + 5, vWidth - 10, [[self class] heightOfText:_someJoke.text]);
    }
    self.collectionImage.frame = CGRectMake(_detailLabel.X, _detailLabel.Y + _detailLabel.Height + 5, imageHeight , imageHeight);
    self.shareImage.frame = CGRectMake(vWidth - vHeight, _collectionImage.Y, imageHeight, imageHeight);
}


@end
